package david.java.tools;

import java.io.StringWriter;

/**
 * @Description:
 * @Author: David
 * @Date: Create in 10:15 上午 2021/11/24
 */
public class FreemarkerUtil {
    private static String SOURCE_PATH = "freemarker/demo.ftl";


    /**
     * 把ftl文件解析成html字符串内容
     * @param templatePath ftl文件路径
     * @param model        模型数据
     * @return 解析后的html文件内容
     */
    // public static String parse(String templatePath, Map<String, Object> model) {
        StringWriter stringWriter = null;
    //     BufferedWriter writer = null;
    //     try {
    //         freemarker.template.Template template = SpringUtils.getBean("freeMarkerConfigurer", FreeMarkerConfigurer.class).getConfiguration().getTemplate(templatePath);
    //         stringWriter = new StringWriter();
    //         writer = new BufferedWriter(stringWriter);
    //         template.process(model, writer);
    //         writer.flush();
    //         return stringWriter.toString();
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     } finally {
    //         IOUtils.closeQuietly(writer);
    //         IOUtils.closeQuietly(stringWriter);
    //     }
    //     return null;
    // }

    /**
     * 解析ftl文件内容
     * @param content  原ftl模板文件内容
     * @param model 模板文件数据
     * @return 解析后的html文件内容
     */
    // public static String parseFtlContent(String content, Map<String, Object> model) {
    //     // 获取配置
    //     StringWriter out = new StringWriter();
    //     try {
    //         new Template("template", new StringReader(content), FreemarkerUtils.getFreeMarkerConfigurer().getConfiguration()).process(model, out);
    //     } catch (TemplateException | IOException e) {
    //         return "";
    //     }
    //     String htmlContent = out.toString();
    //
    //     if (out != null) {
    //         try {
    //             out.close();
    //         } catch (IOException e) {
    //             return "";
    //         }
    //     }
    //     return htmlContent;
    // }

}
